﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using PSE.Framework.Cryptography.Util;
using PSE.Framework.Cryptography;

namespace PSE.Tools.Cryptographer.View
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class CryptographerWindow : Window
    {
        private enum CryptographMode
        {
            DES,
            RSA,
            Rijndael,
            Both
        };


        private byte[] _desCryptoKey;
        private string _rsaCryptoKey;
        private CryptographMode _cryptographMode;


        public CryptographerWindow(byte[] desCryptoKey)
        {
            InitializeComponent();
            _cryptographMode = CryptographMode.DES;
            SetDESCryptoKey(desCryptoKey);
        }

        public CryptographerWindow(string rsaCryptoKey)
        {
            InitializeComponent();
            _cryptographMode = CryptographMode.RSA;
            SetRSACryptoKey(rsaCryptoKey);
        }

        public CryptographerWindow(byte[] desCryptoKey, string rsaCryptoKey)
        {
            InitializeComponent();
            _cryptographMode = CryptographMode.Both;

            //apresentar combo box para seleção de algoritmo de criptografia

            stkCryptoMode.Visibility = Visibility.Visible;

            SetDESCryptoKey(desCryptoKey);
            SetRSACryptoKey(rsaCryptoKey);
        }


        private void SetDESCryptoKey(byte[] desCryptoKey)
        {
            if (desCryptoKey == null)
                throw new ArgumentNullException("desCryptoKey");

            _desCryptoKey = new byte[desCryptoKey.Length];

            desCryptoKey.CopyTo(_desCryptoKey, 0);
        }


        private void SetRSACryptoKey(string rsaCryptoKey)
        {
            if (string.IsNullOrEmpty(rsaCryptoKey))
                throw new ArgumentNullException("rsaCryptoKey");

            _rsaCryptoKey = rsaCryptoKey;
        }

        private void btnCriptografar_Click(object sender, RoutedEventArgs e)
        {
            if (string.IsNullOrEmpty(txtOrigem.Text))
            {
                txtDestino.Text = string.Empty;
                return;
            }
            else
            {
                if (_cryptographMode == CryptographMode.DES)
                    txtDestino.Text = DESCryptographer.Encrypt(txtOrigem.Text, _desCryptoKey);
                else if (_cryptographMode == CryptographMode.RSA)
                    txtDestino.Text = RSACryptographer.EncryptString(txtOrigem.Text, _rsaCryptoKey);
                else if (_cryptographMode == CryptographMode.Rijndael)
                    txtDestino.Text = RijndaelCryptographer.EncryptString(txtOrigem.Text);
                else
                {
                    ComboBoxItem selectedItem = (ComboBoxItem)cboCryptoMode.SelectedItem;

                    if (selectedItem == null)
                        MessageBox.Show("Escolha o algorítmo de criptografia a ser utilizado.");
                    else if (selectedItem.Content.ToString() == "DES")
                        txtDestino.Text = DESCryptographer.Encrypt(txtOrigem.Text, _desCryptoKey);
                    else if (selectedItem.Content.ToString() == "RSA")
                        txtDestino.Text = RSACryptographer.EncryptString(txtOrigem.Text, _rsaCryptoKey);
                    else if (selectedItem.Content.ToString() == "Rijndael")
                        txtDestino.Text = RijndaelCryptographer.EncryptString(txtOrigem.Text);
                }
            }
        }
    }
}
